$(function () {
    $("#jqGrid").Grid({
        url: '../sys/msgmodelinfo/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '编码', name: 'code', index: 'code', width: 80},
			{label: '类型', name: 'typeName', index: 'type', width: 80},
			{label: '名称', name: 'name', index: 'name', width: 80},
			{label: '描述', name: 'content', index: 'content', width: 80},
			{label: 'android跳转链接', name: 'androidUrl', index: 'android_url', width: 80},
			{label: 'ios跳转链接', name: 'iosUrl', index: 'ios_url', width: 80},
			{label: '消息内容', name: 'msg', index: 'msg', width: 80},
			{label: '图标', name: 'icon', index: 'icon', width: 80}]
    });
    vm.getMsgModelType();
});

var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		msgModelInfo: {},
		uploadInfo: {},
		baseImgUrl: "",
		uploadIconList:[],
		msgModelType:[],
		showAuditDetail:true,
		ruleValidate: {
			name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
			code: [{required: true, message: '编码不能为空', trigger: 'blur'}],
			type: [{required: true, message: '类型不能为空', trigger: 'blur'}],
			msg: [{required: true, message: '消息内容不能为空', trigger: 'blur'}]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		getMsgModelType: function () {
            Ajax.request({
                url: '../sys/dict/queryAll',
                params: {
                    'groupCode': 'MESSAGEMODELTYPE'
                },
                async: true,
                successCallback: function (r) {
                    vm.msgModelType = r.list;
                }
            });
        },
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.msgModelInfo = {};
			vm.uploadIconList=[];
			vm.getUploadFileToken();
		},
		update: function (event) {
            var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.uploadIconList=[];
            vm.getInfo(id);
            vm.getUploadFileToken();
		},
		getUploadFileToken:function(){
			Ajax.request({
                url: "../sysUpload/getToken",
                async: true,
                successCallback: function (r) {
                    vm.uploadInfo.token=r.uptoken;
                    vm.uploadInfo.key="msgModel"+(new Date().getTime());
                    vm.baseImgUrl=r.baseImgUrl;
                }
            });
		},
		saveOrUpdate: function (event) {
            var url = vm.msgModelInfo.id == null ? "../sys/msgmodelinfo/save" : "../sys/msgmodelinfo/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.msgModelInfo),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../sys/msgmodelinfo/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../sys/msgmodelinfo/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.msgModelInfo = r.msgModelInfo;
                    vm.uploadIconList.push({name:'icon',url:vm.msgModelInfo.icon,status:'finished'});
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleIconImgBeforeUpload:function(){
        	var check = vm.uploadIconList.length < 1;
        	if (!check) {
        		alert('首先删除原来的图片!');
        	}
        	return check;
        },
        handleImgFormatError:function(file){
        	alert('图片格式错误！支持图片格式为:jpg,png,jpeg!');
        },
        handleImgMaxSize:function(file){
        	alert('图片最大大小为4M!');
        },
        handleSuccessIcon:function(res, file){
        	vm.uploadIconList.push({name:file.name,url:vm.baseImgUrl+res.key,status:'finished'});
        	vm.msgModelInfo.icon=vm.baseImgUrl+res.key;
        },
        handleView:function(file){
        	openWindow({
            	type: 2,
                title: "图片显示",
                area: ['80%', '60%'],
                content: [file.url],
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                    layer.close(index);
                }
            });
        },
        handleRemove:function(){
        	vm.uploadIconList=[];
        }
	}
});